var baseURL = document.location.origin;
var qfcode = "";
//GetSystem() 
function GetSystem() {
    //平台、设备和操作系统
    var system = {
        win: false,
        mac: false
    };
    //检测平台
    var p = navigator.platform;
    system.win = p.indexOf("Win") == 0;
    system.mac = p.indexOf("Mac") == 0;
    system.linux = p.indexOf("Linux") == 0;
    if (!navigator.platform === "Linux armv8l") {
        baseURL = document.location.origin;
    } else {
        baseURL = document.location.hostname
    }
}

var arr = location.search.split('=');
if (arr.length > 1) {
    qfcode = arr[1];
}


function addJs(src) {
    var newScript = document.createElement("script");
    newScript.type = "text/javascript";
    newScript.src = src;
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(newScript);
}
// 窗口大小变化 resize
function EcharResize(EchartsName) {
    window.onresize = function() {
        // console.dir(EchartsName)
        EchartsName.forEach(element => {
            echarts.init(element).resize()
        })
    }
}

// https://www.cnblogs.com/aidixie/p/12741193.html
function groupBy(array, id) {
    let groups = {};
    array.forEach(function(o) {
        let group = JSON.stringify(o[id]);
        groups[group] = groups[group] || [];
        groups[group].push(o);
    });
    return Object.values(groups);
}

if (!baseURL.includes("127.0.0.1")) {
    getQfcode();
}


function getQfcode() {
    if (qfcode == "") {
        $.ajax({
            url: "/Request/ashx/DataBase.ashx?type=getcur",
            dataType: 'json',
            async: false,
            success: function(data) {
                qfcode = data.Data.QfCode;
            }
        });
    }

}

function mockData(code, str) {
    let url = ''
        // 测试环境下 默认使用mock
    if (baseURL.includes("127.0.0.1")) {
        switch (str) {
            case 'Goods':
                url = 'http://Goods.test.com' //产品
                break;
            case 'value':
                url = "http://value.test.com" //价值
                break;
            case 'dev':
                url = "http://value.sale.com" //销售
                break;
            case 'schedule':
                url = "http://schedule.test.com" //日程
                break;
            default:
                url = baseURL + "/Request/ashx/V_DataSource.ashx?type=getdata&dbCode=" + code + "&qfcode=" + qfcode; // 正式
                break;
        }
    } else {
        url = baseURL + "/Request/ashx/V_DataSource.ashx?type=getdata&dbCode=" + code + "&qfcode=" + qfcode; // 正式
    }
    return url;
}
// Get Date
function GetCodeData(code, type, str, data) {
    let url = mockData(code, str);
    return new Promise((res, rej) => {
        $.ajax({
            async: true,
            url,
            type,
            dataType: 'json',
            data
        }).then(function(data) {
            res(data)
        }).fail(function(e) {
            // console.dir(e);
        })
    })
}

function GetData(url, type, data) {
    return new Promise((res, rej) => {
        $.ajax({
            async: true,
            url,
            type,
            data
        }).then(function(data) {
            res(data)
        }).fail(function(e) {
            console.dir(e);
        })
    })
}
// 表格自动滚动
function TableAotoRoll(list_refs) {
    list_refs.forEach(element => {
        // const table = element
        const divData = element.bodyWrapper
            // 增加距离顶部距离
        setInterval(() => {
            // 自增距离像素
            divData.scrollTop += 1
                // 判断元素是否滚动到底部(可视高度+距离顶部=整个高度)
            if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
                // 重置顶部距离
                divData.scrollTop = 0
            }
        }, 150)
    });
}

function Autoscroll(app, speed, ismouse) {
    var MyMar = setInterval(() => {
        if (app.clientHeight + app.scrollTop == app.scrollHeight) {
            app.scrollTop = 0
        } else {
            app.scrollTop++
        }
    }, speed)
    if (ismouse) {
        app.onmouseover = function() {
            clearInterval(MyMar)
        }
        app.onmouseout = function() {
            MyMar = setInterval(Marquee, speed)
        }
    }
}